{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 第 8 节　假设检验\n",
    "## 第 3 章　使用 Python 进行数据分析｜用 Python 动手学统计学"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 13. t 检验的实现：环境准备"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "# 用于数值计算的库\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "# 用于绘图的库\n",
    "import seaborn as sns\n",
    "from scipy import stats\n",
    "\n",
    "sns.set()\n",
    "\n",
    "# 设置浮点数打印精度\n",
    "%precision 3\n",
    "# 在 Jupyter Notebook 里显示图形\n",
    "%matplotlib inline"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:02:29.417494Z",
     "end_time": "2024-04-16T20:02:29.447597Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "0    58.529820\n1    52.353039\n2    74.446169\n3    52.983263\n4    55.876879\nName: weight, dtype: float64"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读入数据\n",
    "junk_food = pd.read_csv(\n",
    "    \"3-8-1-junk-food-weight.csv\")[\"weight\"]\n",
    "junk_food.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:02:29.446992Z",
     "end_time": "2024-04-16T20:02:29.463535Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 14. t 检验的实现：计算 t 值"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "55.385"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 样本均值\n",
    "mu = np.mean(junk_food)\n",
    "mu"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:02:29.463535Z",
     "end_time": "2024-04-16T20:02:29.501709Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "19"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 自由度\n",
    "df = len(junk_food) - 1\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:02:29.479356Z",
     "end_time": "2024-04-16T20:02:29.501709Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "1.958"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标准误差\n",
    "sigma = np.std(junk_food, ddof=1)\n",
    "se = sigma / np.sqrt(len(junk_food))\n",
    "se"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:02:29.490306Z",
     "end_time": "2024-04-16T20:02:29.501709Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "2.750"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# t 值\n",
    "t_value = (mu - 50) / se\n",
    "t_value"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:02:29.506102Z",
     "end_time": "2024-04-16T20:02:29.578126Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 15. t 检验的实现：计算 p 值"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "0.013"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# p 值\n",
    "alpha = stats.t.cdf(t_value, df=df)\n",
    "(1 - alpha) * 2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:02:29.532500Z",
     "end_time": "2024-04-16T20:02:29.585102Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "TtestResult(statistic=2.7503396831713434, pvalue=0.012725590012524155, df=19)"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# t 检验\n",
    "stats.ttest_1samp(junk_food, 50)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:02:29.555966Z",
     "end_time": "2024-04-16T20:02:29.585102Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 16. 通过模拟实验计算 p 值"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [
    "# 样本的相关信息 (一部分)\n",
    "size = len(junk_food)\n",
    "sigma = np.std(junk_food, ddof=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:02:29.583441Z",
     "end_time": "2024-04-16T20:02:29.638790Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [],
   "source": [
    "# 存放 t 值的窗口\n",
    "t_value_array = np.zeros(50000)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:02:29.605388Z",
     "end_time": "2024-04-16T20:02:29.638790Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [],
   "source": [
    "# 总体均值为 50, 以接受零假设为前提进行 50,000 次抽样并计算 t 值的实验\n",
    "np.random.seed(1)\n",
    "norm_dist = stats.norm(loc=50, scale=sigma)\n",
    "for i in range(0, 50000):\n",
    "    sample = norm_dist.rvs(size=size)\n",
    "    sample_mean = np.mean(sample)\n",
    "    sample_std = np.std(sample, ddof=1)\n",
    "    sample_se = sample_std / np.sqrt(size)\n",
    "    t_value_array[i] = (sample_mean - 50) / sample_se"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:02:29.623157Z",
     "end_time": "2024-04-16T20:02:32.316286Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "0.013"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(sum(t_value_array > t_value) / 50000) * 2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:02:32.316286Z",
     "end_time": "2024-04-16T20:02:32.334655Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:02:32.334655Z",
     "end_time": "2024-04-16T20:02:32.382796Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
